Skip to content

Fix forc-fmt empty if-body + else collapsing onto one line#7628

Open
Dnreikronos wants to merge 4 commits into
FuelLabs:masterfrom
Dnreikronos:Dnreikronos/fix-forc-fmt-empty-if-else-collapse
Open

Fix forc-fmt empty if-body + else collapsing onto one line#7628
Dnreikronos wants to merge 4 commits into
FuelLabs:masterfrom
Dnreikronos:Dnreikronos/fix-forc-fmt-empty-if-else-collapse

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

Description

Fixes #7625

forc-fmt collapses empty if-body + else onto a single line:

// Before (wrong):
if __size_of::<T>() == 0 {    } else if __is_reference_type::<T>() {

// After (correct):
if __size_of::<T>() == 0 {
} else if __is_reference_type::<T>() {

Root cause: In format_then_block, when the then-block is empty and has an else clause, block_unindent() was called but no newline was written — so the closing } from format_else_opt ended up on the same line.

Fix: Write a newline after the empty body when an else clause exists, and skip block_unindent since close_curly_brace in format_else_opt already handles unindentation. Guarded with LineStyle::Inline to avoid injecting newlines during width measurement probes.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

When an if-block has an empty body and an else clause, the formatter
wrote no newline before the closing brace, producing collapsed output
like `if cond {    } else if ...` on a single line.

Write a newline after the empty body when an else clause exists, and
skip block_unindent since close_curly_brace in format_else_opt already
handles it. Guard with LineStyle::Inline to avoid injecting newlines
during width measurement probes.

Closes FuelLabs#7625
Add expr-level test for empty if + else-if and integration tests for
empty if + else-if and empty if + else with long lines that prevent
inlining.
@Dnreikronos Dnreikronos requested review from a team as code owners May 23, 2026 18:38
@cursor
Copy link
Copy Markdown

cursor Bot commented May 23, 2026

PR Summary

Low Risk
Formatter-only logic plus tests and a cosmetic stdlib reformat; no runtime or security behavior changes.

Overview
forc-fmt no longer prints an empty if body and the following else / else if on one line (e.g. if cond { } else if …). In format_then_block, when the then-block is empty and an else exists, the formatter now emits a newline before the closing brace is handled in format_else_opt, and it skips block_unindent in that case so brace layout stays correct. The behavior is not applied when LineStyle::Inline is active, so width-probe formatting stays stable.

Regression coverage was added in expression and integration tests. sway-lib-std raw_ptr::write was reformatted to the expanded multi-line if/else if shape as a real-world example of the fix.

Reviewed by Cursor Bugbot for commit 12b35b7. Bugbot is set up for automated code reviews on this repo. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

forc-fmt issue on raw_ptr::write.

1 participant